package com.bestom.bluetoothlock.bluetooth;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothManager;
import android.content.Context;


import com.bestom.bluetoothlock.util.LogMsg;

import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.binary.Hex;

public class SendRequest {
    public static final String INSTRUCTIONS_HEAD = "A9";
    public static final String INSTRUCTIONS_INQUIRE_LOCK_STATE = "0F";
    BluetoothAdapter bluetoothAdapter = this.bluetoothManager.getAdapter();
    BluetoothManager bluetoothManager;
    private Context mContext;

    public SendRequest(Context context) {
        this.mContext = context;
        this.bluetoothManager = (BluetoothManager) context.getSystemService(Context.BLUETOOTH_SERVICE);
    }

    public static byte[] sendOpen() {
        byte[] bArr = new byte[5];
        bArr[0] = -95;
        bArr[1] = 1;
        bArr[2] = 0;
        bArr[3] = 0;
        bArr[4] = (byte) getVerify(bArr);
        return bArr;
    }

    public static byte[] sendRegisterFin(int i) {
        byte[] bArr = new byte[5];
        bArr[0] = -95;
        bArr[1] = 2;
        bArr[2] = 1;
        bArr[3] = (byte) i;
        bArr[4] = (byte) getVerify(bArr);
        return bArr;
    }

    public static byte[] sendCancelRegister() {
        byte[] bArr = new byte[5];
        bArr[0] = -95;
        bArr[1] = 3;
        bArr[2] = 0;
        bArr[3] = 0;
        bArr[4] = (byte) getVerify(bArr);
        return bArr;
    }

    public static byte[] sendClearFinger() {
        byte[] bArr = new byte[5];
        bArr[0] = -95;
        bArr[1] = 4;
        bArr[2] = 0;
        bArr[3] = 0;
        bArr[4] = (byte) getVerify(bArr);
        return bArr;
    }

    public static byte[] sendFinNumber() {
        byte[] bArr = new byte[5];
        bArr[0] = -95;
        bArr[1] = 5;
        bArr[2] = 0;
        bArr[3] = 0;
        bArr[4] = (byte) getVerify(bArr);
        return bArr;
    }

    public static byte[] sendPower() {
        byte[] bArr = new byte[5];
        bArr[0] = -95;
        bArr[1] = 6;
        bArr[2] = 0;
        bArr[3] = 0;
        bArr[4] = (byte) getVerify(bArr);
        return bArr;
    }

    public static byte[] sendSingleFin(int i) {
        byte[] bArr = new byte[5];
        bArr[0] = -95;
        bArr[1] = 7;
        bArr[2] = 1;
        bArr[3] = (byte) i;
        bArr[4] = (byte) getVerify(bArr);
        return bArr;
    }

    public static byte[] sendBleStrength() {
        byte[] bArr = new byte[5];
        bArr[0] = -95;
        bArr[1] = 8;
        bArr[2] = 0;
        bArr[3] = 0;
        bArr[4] = (byte) getVerify(bArr);
        return bArr;
    }

    public static byte[] sendRegisterResult() {
        byte[] bArr = new byte[5];
        bArr[0] = -95;
        bArr[1] = 9;
        bArr[2] = 0;
        bArr[3] = 0;
        bArr[4] = (byte) getVerify(bArr);
        return bArr;
    }

    public static byte[] sendBluetoothMode() {
        byte[] bArr = new byte[5];
        bArr[0] = -95;
        bArr[1] = 10;
        bArr[2] = 1;
        bArr[3] = 1;
        bArr[4] = (byte) getVerify(bArr);
        return bArr;
    }

    public static byte[] sendAloneMode() {
        byte[] bArr = new byte[5];
        bArr[0] = -95;
        bArr[1] = 10;
        bArr[2] = 1;
        bArr[3] = 0;
        bArr[4] = (byte) getVerify(bArr);
        return bArr;
    }

    public static byte[] sendSaveFin(int i) {
        byte[] bArr = new byte[5];
        bArr[0] = -95;
        bArr[1] = 11;
        bArr[2] = 1;
        bArr[3] = (byte) i;
        bArr[4] = (byte) getVerify(bArr);
        return bArr;
    }

    public static byte[] sendInquireSave() {
        byte[] bArr = new byte[5];
        bArr[0] = -95;
        bArr[1] = 12;
        bArr[2] = 0;
        bArr[3] = 0;
        bArr[4] = (byte) getVerify(bArr);
        return bArr;
    }

    public static byte[] sendReset() {
        byte[] bArr = new byte[5];
        bArr[0] = -95;
        bArr[1] = 13;
        bArr[2] = 0;
        bArr[3] = 0;
        bArr[4] = (byte) getVerify(bArr);
        return bArr;
    }

    public static byte[] sendOpenRadar() {
        byte[] bArr = new byte[5];
        bArr[0] = -95;
        bArr[1] = 14;
        bArr[2] = 1;
        bArr[3] = 0;
        bArr[4] = (byte) getVerify(bArr);
        return bArr;
    }

    public static byte[] sendCloseRadar() {
        byte[] bArr = new byte[5];
        bArr[0] = -95;
        bArr[1] = 14;
        bArr[2] = 1;
        bArr[3] = 1;
        bArr[4] = (byte) getVerify(bArr);
        return bArr;
    }

    public static byte[] sendLcokState() {
        try {
            return Hex.decodeHex(getZeroLenthInstructions("0F").toCharArray());
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    private static int getVerify(byte[] bArr) {
        int i = 0;
        for (byte b : bArr) {
            i += b;
        }
        return i & 255;
    }

    private static String getZeroLenthInstructions(String str) {
        StringBuffer stringBuffer = new StringBuffer(INSTRUCTIONS_HEAD);
        stringBuffer.append(str);
        stringBuffer.append("00");
        stringBuffer.append(calculationCRC(stringBuffer.toString()));
        proofreadingCRC(stringBuffer.toString());
        return stringBuffer.toString();
    }

    private static String getOneLenthInstructions(String str, String str2) {
        StringBuffer stringBuffer = new StringBuffer(INSTRUCTIONS_HEAD);
        stringBuffer.append(str);
        stringBuffer.append("00");
        stringBuffer.append(BraceletInstructions.INSTRUCTIONS_CHANGE_LOCK_STATE);
        stringBuffer.append(str2);
        stringBuffer.append(calculationCRC(stringBuffer.toString()));
        proofreadingCRC(stringBuffer.toString());
        return stringBuffer.toString();
    }

    public static String getNLenthInstructions(String str, String... strArr) {
        if (strArr == null || strArr.length < 0) {
            return "";
        }
        StringBuffer stringBuffer = new StringBuffer(INSTRUCTIONS_HEAD);
        stringBuffer.append(str);
        stringBuffer.append("00");
        stringBuffer.append(hex10_16(strArr.length + ""));
        for (String append : strArr) {
            stringBuffer.append(append);
        }
        stringBuffer.append(calculationCRC(stringBuffer.toString()));
        proofreadingCRC(stringBuffer.toString());
        return stringBuffer.toString();
    }

    public static String calculationCRC(String str) {
        String str2 = "";
        try {
            byte[] decodeHex = Hex.decodeHex(str.toCharArray());
            if (decodeHex != null) {
                byte b = decodeHex[0];
                for (int i = 1; i < decodeHex.length; i++) {
                    b = (byte) (b + decodeHex[i]);
                }
                byte b2 = (byte) (b & 255);
                str2 = Integer.toHexString(b2) + "";
            }
            if (str2.length() >= 2) {
                return str2;
            }
            return "0" + str2;
        } catch (DecoderException e) {
            e.printStackTrace();
            return str2;
        }
    }

    public static String calculationCRC(String str, byte[] bArr) {
        if (bArr == null || bArr.length <= 0) {
            return "";
        }
        try {
            byte[] decodeHex = Hex.decodeHex(str.toCharArray());
            byte[] bArr2 = new byte[(decodeHex.length + bArr.length)];
            System.arraycopy(decodeHex, 0, bArr2, 0, decodeHex.length);
            System.arraycopy(bArr, 0, bArr2, decodeHex.length, bArr.length);
            byte b = bArr2[0];
            for (int i = 1; i < bArr2.length; i++) {
                b = (byte) (b + bArr2[i]);
            }
            String str2 = Integer.toHexString(b & 255) + "";
            if (str2.length() >= 2) {
                return str2;
            }
            return "0" + str2;
        } catch (DecoderException e) {
            e.printStackTrace();
            return "";
        }
    }

    public static boolean proofreadingCRC(String str) {
        if (str == null || str.length() <= 2) {
            LogMsg.Mess("校验的数据:" + str + "");
            return false;
        }
        char[] charArray = str.substring(0, str.length() - 2).toCharArray();
        String substring = str.substring(str.length() - 2);
        String str2 = "";
        try {
            byte[] decodeHex = Hex.decodeHex(charArray);
            if (decodeHex != null) {
                byte b = decodeHex[0];
                for (int i = 1; i < decodeHex.length; i++) {
                    b = (byte) (b + decodeHex[i]);
                }
                str2 = Integer.toHexString(b & 255) + "";
                if (str2.length() < 2) {
                    str2 = "0" + str2;
                }
            }
            LogMsg.Mess("校对一下:" + str);
            if (substring.equals(str2)) {
                return true;
            }
            return false;
        } catch (DecoderException e) {
            e.printStackTrace();
        }
        return false;
    }

    public static String hex10_16(String str) {
        String str2 = "00";
        try {
            str2 = Integer.toHexString(Integer.parseInt(str));
            if (str2.length() % 2 != 0) {
                str2 = "0" + str2;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        LogMsg.Mess("进制转换:" + str2);
        return str2;
    }
}